package sse.ustc.cc.fragment;

import java.util.ArrayList;
import java.util.List;

import sse.ustc.cc.activity.R;
import sse.ustc.cc.custom.AddCourseTimeView;
import sse.ustc.cc.db.DBService;
import sse.ustc.cc.util.AddCourseInfo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * 添加课程 fragment
 * @author xuhao
 *
 */
public class AddCourseFrag extends Fragment {

	private EditText courseName = null;//课程名称
	private EditText courseRoom = null;//课程地点
	private EditText courseteacher = null;//课程老师
	private Context mContext;
	private Button readdtime = null;//再次添加时间按钮
	private Button addcourseok = null;//确定添加课程按钮
	private LinearLayout liner;
	//课程信息对象
	public AddCourseInfo addcourseInfo = new AddCourseInfo();
	//课程时间list
	private List<AddCourseTimeView> addtimeList = new ArrayList<AddCourseTimeView>();

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		mContext = activity;
		// DBService ds = new DBService(this);
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		final View rootView = inflater.inflate(R.layout.view_edit_schedule,
				container, false);// 关联布局文件
		//获得数据库服务
		final DBService ds = new DBService(mContext);
		
		courseName = (EditText) rootView.findViewById(R.id.course_name);
		courseRoom = (EditText) rootView.findViewById(R.id.course_room);
		courseteacher = (EditText) rootView.findViewById(R.id.course_teacher);

		readdtime = (Button) rootView.findViewById(R.id.readdtime);
		addcourseok = (Button) rootView.findViewById(R.id.addcourseok);
		
		liner = (LinearLayout) rootView.findViewById(R.id.liner);
		
		/**默认有一个课程时间段*/
		//声明一个AddCourseTimeView对象
		LinearLayout liner0 = new LinearLayout(getActivity());
		AddCourseTimeView addtime = new AddCourseTimeView(getActivity());
		Button delTime = new Button(getActivity());
		delTime.setText("删除");
		delTime.setOnClickListener(new DelTime());
		liner0.addView(delTime);
		liner0.addView(addtime);
		final int indexStart = liner.getChildCount()-1;
		liner.addView(liner0, indexStart);
		
		// addcourseInfo.getCourseTimeList().set(0, addtime.getCourseTime());

		readdtime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 点击添加其他时间段按钮，添加时间段
				LinearLayout liner0 = new LinearLayout(getActivity());
				AddCourseTimeView addtime = new AddCourseTimeView(getActivity());
				Button delTime = new Button(getActivity());
				delTime.setText("删除");
				delTime.setOnClickListener(new DelTime());
				liner0.addView(delTime);
				liner0.addView(addtime);
				int num = liner.getChildCount();
				liner.addView(liner0, num-1);
			}

		});
		/**
		 * 确定添加课程
		 */
		addcourseok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 将课程信息添加到addcourseInfo
				addcourseInfo.setCourseName(courseName.getText().toString());
				addcourseInfo.setCourseRoom(courseRoom.getText().toString());
				addcourseInfo.setCourseTeacher(courseteacher.getText()
						.toString());
				Log.e("CourseName", courseName.getText().toString());
				Log.e("CourseRoom", courseName.getText().toString());
				Log.e("CourseTeacher", courseName.getText().toString());
				
				int num = liner.getChildCount();
				for(int i = indexStart;i<num-1;i++){
					LinearLayout l = (LinearLayout) liner.getChildAt(i);
					addtimeList.add((AddCourseTimeView) l.getChildAt(1));
				}
				
				Log.e("addtimeList", addtimeList.size() + "");
				ds.insertTeacher(courseteacher.getText().toString(), "", "",
						"", "", "", "");
				for (int i = 0; i < addtimeList.size(); i++) {
					addcourseInfo.getCourseTimeList().add(i,
							addtimeList.get(i).getCourseTime());
				}
				// 输出
				Log.e("addtimeList", addcourseInfo.getCourseTimeList().size()
						+ "");
				for (int i = 0; i < addcourseInfo.getCourseTimeList().size(); i++) {
					String week_begin = addcourseInfo.getCourseTimeList()
							.get(i).getStartWeek()
							+ "";
					String week_end = addcourseInfo.getCourseTimeList().get(i)
							.getEndWeek()
							+ "";
					String week = addcourseInfo.getCourseTimeList().get(i)
							.getWeekJieShu();
					week=week(week);
					String s_begin = addcourseInfo.getCourseTimeList().get(i)
							.getStartJieShu()
							+ "";
					String s_end = addcourseInfo.getCourseTimeList().get(i)
							.getEndJieShu()
							+ "";
					String c_begin = addcourseInfo.getCourseTimeList().get(i)
							.getStartTime();
					Log.e("c_begin",addcourseInfo.getCourseTimeList().get(i)
							.getStartTime());
					String c_end = addcourseInfo.getCourseTimeList().get(i)
							.getEndTime();
					Log.e("c_end",addcourseInfo.getCourseTimeList().get(i)
							.getEndTime());
					String c_alarm = "15";
					String c_exam = "15";
					
					String c_other = "幽默";
					ds.insertCourse(courseName.getText().toString(), week,
							s_begin, week_begin, courseteacher.getText()
									.toString(), c_alarm, c_exam, c_other,
							courseRoom.getText().toString(), s_end,
							week_end, c_begin, c_end);
					Toast.makeText(mContext, "insert success!", Toast.LENGTH_SHORT).show();
					// 存入数据库
				}

			}
		});

		return rootView;
	}
	
	class DelTime implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			liner.removeView((View) arg0.getParent());
		}
		
	} 
	
	public String week(String weektiem) {
		String weekafter = null;
		if(weektiem.endsWith("周一")){
			weekafter = "1";
		}else if(weektiem.endsWith("周二")){
			weekafter = "2";
		}else if(weektiem.endsWith("周三")){
			weekafter = "3";
		}else if(weektiem.endsWith("周四")){
			weekafter = "4";
		}else if(weektiem.endsWith("周五")){
			weekafter = "5";
		}else if(weektiem.endsWith("周六")){
			weekafter = "6";
		}else if(weektiem.endsWith("周日")){
			weekafter = "7";
		}else{
			Toast.makeText(mContext, "error", Toast.LENGTH_SHORT).show();
		}
		return weekafter;

	}
	public void onBackPressed() {
		DayClassInfoFrag day=new DayClassInfoFrag();
		day.getFragmentManager().notify();
	return;
	}
}
